package team.scau.laboratory_repair.common.quartz.jobs.repair;

import lombok.extern.apachecommons.CommonsLog;
import org.activiti.engine.TaskService;
import org.activiti.engine.delegate.DelegateTask;
import org.activiti.engine.task.Task;
import org.quartz.JobDataMap;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.quartz.SchedulerException;
import org.springframework.scheduling.quartz.QuartzJobBean;
import team.scau.laboratory_repair.service.JobService;
import team.scau.laboratory_repair.common.utils.spring.SpringContextHolder;
import team.scau.laboratory_repair.service.WeiXinService;

import java.util.List;

/**
 * @author 30254
 * creadtedate:2018/10/15
 */
@CommonsLog
public class DelegateJob extends QuartzJobBean {

    @Override
    protected void executeInternal(JobExecutionContext context) throws JobExecutionException {
        JobDataMap dataMap = context.getMergedJobDataMap();
        WeiXinService weiXinService = SpringContextHolder.getBean("weiXinService");
        JobService jobService = SpringContextHolder.getBean("jobService");
        TaskService taskService = SpringContextHolder.getBean("taskService");
        String taskId = (String) dataMap.get("taskId");
        List<Task> list = taskService.createTaskQuery().taskId(taskId).list();
        if (list.size() > 0) {
            Task task = list.get(0);
            if (task.getAssignee()==null){
                weiXinService.infoRepairManagers();
                return;
            }
            try {
                jobService.removeSchedule(taskId);
            } catch (SchedulerException e) {
                log.error(e.getMessage(), e);
            }
        }
//        log.info("delegate job executed");
    }
}
